package com.jia.netty.handler;


import com.jia.entity.ChatMessage;
import com.jia.entity.netty.ChartMsg;
import com.jia.service.ChatMessageService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

public class ChartHandler extends SimpleChannelInboundHandler<ChartMsg> {



    private RabbitTemplate rabbitTemplate;

    private ChatMessageService messageService;

    public ChartHandler(RabbitTemplate rabbitTemplate, ChatMessageService messageService) {
        this.rabbitTemplate = rabbitTemplate;
        this.messageService = messageService;
    }


    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ChartMsg chartMsg) throws Exception {
        System.out.println("聊天的信息：" + chartMsg);
        String tid = chartMsg.getTid();
        Integer type = chartMsg.getType();

        // 消息入库
        ChatMessage message = new ChatMessage();
        message.setFromId(chartMsg.getFid());
        message.setToId(chartMsg.getTid());
        message.setType(1);
        message.setMsg(chartMsg.getContent());
        messageService.save(message);
        // 转发出去

        rabbitTemplate.convertAndSend("ws_exchange","",chartMsg);


    }

}
